/**
 * Created by PC on 2016/1/18.
 */
class Disposer {

    public name:string;
    public parent:Disposer;
    private context:any;
    private children:Disposer[];
    private eventListeners:any[];
    private reses:any[];

    public constructor(context:any) {
        this.context = context;
        this.name = egret.getQualifiedClassName(this)+Math.random();
        this.children = [];
        this.eventListeners = [];
        this.reses = [];
    }

    public getRes(name:string,callback?:Function,thisObject?:any) {
        this.addRes(name);
        return ResUtils.getRes(name,callback,thisObject);
    }

    public addRes(name):void {
        if(this.reses.length == 0){
            this.reses.push(name);
        }else{
            var has = this.reses.indexOf(name);
            if(has != -1){
                this.reses.push(name);
            }
        }
    }

    public newBitmap(name:string) {
        var bm = new egret.Bitmap();
        var res = this.getRes(name,function(r){
            bm.texture = r;
        },this);
        if(res){
            bm.texture = res;
        }
        return bm;
    }

    public newScale9Grid(name: string,w: number,h: number,rect: egret.Rectangle,x: number = 0,y: number = 0):egret.Bitmap {
        var bm = new egret.Bitmap();
        bm.scale9Grid = rect;
        bm.width = w;
        bm.height = h;
        bm.x = x;
        bm.y = y;
        var res = this.getRes(name,function(r){
            bm.texture = r;
        },this);
        if(res){
            bm.texture = res;
        }
        return bm;
    }

    public dispose():void {
        this.disposeChildren();
        this.disposeSelf();
        this.parent = null;
    }

    public disposeSelf():void {
        this.disposeAllTweens();
        this.disposeEventListeners();
        this.disposeReses();
    }

    public addChild(d:Disposer):void {
        this.children.push(d);
    }

    public removeChild(d:Disposer):void {
        this.children.forEach((c,index) =>{
            if(d == c){
                this.children.splice(index,1);
            }
        })
    }

    public addEventListener(context:any,type:string,listener:Function,thisObject:any,useCapture?:boolean,priority?:number):void {
        if(this.hasEvenListener(context,type,listener,thisObject,useCapture,priority) == -1){
            context.addEventListener(type,listener,thisObject,useCapture,priority);
            this.eventListeners.push({
                dispatcher: context,
                type: type,
                listener: listener,
                thisObject: thisObject,
                useCapture: useCapture
            });
        }else{
//            console.log("has same event");
        }
    }

    public hasEvenListener(context: any,type: string,listener: Function,thisObject: any,useCapture?: boolean,priority?: number): number {
        var has = -1;
        this.eventListeners.forEach((d,index) => {
            if(context == d.dispatcher && type == d.type && listener == d.listener && thisObject == d.thisObject){
                has = index;
                return has
            }
        },this);

        return has;
    }

    public removeEventListener(context:any,type:string,listener:Function,thisObject:any,useCapture?:boolean,priority?:number):void {
        context.removeEventListener(type,listener,thisObject,useCapture,priority);
        var has = this.hasEvenListener(context,type,listener,thisObject,useCapture,priority);
        if(has != -1){
            this.eventListeners.splice(has,1);
        }else{
//            console.log("找不到该事件监听");
        }

    }

    public disposeEventListeners():void {
        this.eventListeners.forEach((d) =>{
            //console.log("移除事件监听:"+d.dispatcher+" - "+d.type);
            d.dispatcher.removeEventListener(d.type,d.listener,d.thisObject,d.useCapture);
        },this);
        this.eventListeners = [];
    }

    public disposeRes(name:string):void {
        RES.destroyRes(name);
    }

    public disposeReses():void {
        this.reses.forEach((name) =>{
            //console.log("destroy:"+name);
            RES.destroyRes(name);
        },this);
        this.reses = [];
    }

    public disposeAllTweens():void {
//        console.log("移除动画");
        egret.Tween.removeTweens(this.context);
    }

    public disposeChildren():void {
        this.children.forEach((d) =>{
            d.dispose();
        },this);
        this.children = [];
    }


}


















